package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Cart;
import csu.webcourse.mypetstore.domain.CartItem;
import csu.webcourse.mypetstore.service.CartService;
import csu.webcourse.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

public class UpdateCartServlet extends HttpServlet {
    private static final String CART_FORM = "/WEB-INF/jsp/cart/cart.jsp";
    CartService cartService;
    int quantity;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId=req.getParameter("userId");
        String itemId=req.getParameter("itemId");
        int quantity=Integer.parseInt(req.getParameter("quantity"));
        System.out.println(userId+itemId+quantity);
        HttpSession session = req.getSession();
        cartService = new CartService();
        Cart cart = (Cart) session.getAttribute("cart");
        if(cart!=null) {
            Iterator<CartItem> cartItems = cartService.getAllCartItems(cart);

            //用户登陆后须对数据库进行操作
            if (userId != null && !userId.equals("")) {
                //更新数据库(删除userId对应信息后重新插入
                cartService.removeCartListASYNC(userId,itemId);
            }

                try {
                    cartService.setQuantityByItemId(cart, itemId, quantity);
                    if (quantity < 1) {
                        cartItems.remove();
                    } else {
                        this.quantity = quantity;
                    }
                    //重新插入
                    if (userId != null && !userId.equals("")) {
                        while (this.quantity-- > 0) {
                            cartService.insertCartList(itemId, userId);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            session.setAttribute("cart",cart);

            String quantityAll="";
            quantityAll=itemId+","+quantity;

            resp.setContentType("text/plain");
            PrintWriter out= resp.getWriter();
            out.write(quantityAll);
        }
        else {
            cart=new Cart();
            session.setAttribute("cart",cart);
        }

        Account account = (Account) session.getAttribute("loginAccount");
        if (account != null) {
            HttpServletRequest httpRequest = req;
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
            String logInfo = logService.logInfo(" ") + strBackUrl + "用户更新购物车";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

//        req.getRequestDispatcher(CART_FORM).forward(req, resp);
    }
}
